% # variable:
% # eroot, bookuuid, max_level

% import uuid
% import datetime as DT
% from lonote.civa.tz import local_timezone

% def get_title(enote):
    % title = enote.findtext('./title')
    % return title
% end

% def get_path(enote, bookuuid):
    % path = '/site/{bookuuid}/{uuid}/'.format(bookuuid = bookuuid, uuid = enote.get('uuid'))
    % return path
% end

% now = DT.datetime.now(tz = local_timezone())

% time_format = '%Y-%m-%d'
% im_time_format = '%Y-%m-%dT%H:%M:%S.%f%z'

% def get_mtime(enote):
    % mtime = DT.datetime.strptime(enote.findtext('modified_time'), im_time_format)
    % desc = ''
    % delta = now - mtime

    % if delta.days == 0:
        % desc = ' (Today)'
    % elif delta.days == 1:
        % desc = ' (Yesterday)'
    % elif 2 <= delta.days <= 29:
        % desc = ' ({} days ago)'.format(delta.days)
    % elif 30 <= delta.days <= 364:
        % desc = ' ({} months ago)'.format(delta.days // 30)
    % else:
        % desc = ' ({} years ago)'.format(delta.days // 365)
    % end

    % return mtime.strftime(time_format) + desc
% end


% def multi_level_menu(enote, level):
    % def recu(enote, level):
        % if level < 0:
            % return
        % end

        % title = get_title(enote)
        % path = get_path(enote, bookuuid)
        % name = uuid.uuid5(uuid.UUID(bookuuid), enote.get('uuid'))

        % esubnotes = enote.findall('./note');

        % if len(esubnotes):
            <li><input type="checkbox" name="{{name}}" id="{{name}}" /><label for="{{name}}"></label><a title="Modified Time: {{get_mtime(enote)}}" href="{{path}}">{{title}}</a>
        % else:
            <li class="leaf"><a title="Modified Time: {{get_mtime(enote)}}" href="{{path}}">{{title}}</a>
        % end

        % level = level -1
        % if len(esubnotes) and not (level < 0) :
            <ul class="sortable">
                %for esub in esubnotes:
                    %recu(esub, level)
                %end
            </ul>
        % end
        </li>
    % end

    % recu(enote, level)
% end

% multi_level_menu(eroot, max_level)
